﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using NLog;

namespace TfsGiggle.Core
{
    static class Settings
    {
        static LoggerWithEventId _logger = (LoggerWithEventId)LogManager.GetCurrentClassLogger(typeof(LoggerWithEventId));

        static int GetInt(string name, int defaultValue)
        {
            var stringValue = ConfigurationManager.AppSettings.Get(name);
            int value;
            if (int.TryParse(stringValue, out value))
            {
                return value;
            }
            return defaultValue;
        }

        static string GetString(string name, string defaultValue)
        {
            var stringValue = ConfigurationManager.AppSettings.Get(name);
            return (string.IsNullOrWhiteSpace(stringValue) ? defaultValue : stringValue);
        }

        static Regex GetRegex(string name, string defaultValue)
        {
            try
            {
                return new Regex(GetString(name, defaultValue), RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            }
            catch (Exception ex)
            {
                _logger.ErrorExceptionWithEventId(EventId.InvalidRegex, string.Format("Please check the regex:'{0}' in the config file", name), ex);
                throw;
            }
        }

        /// <summary>
        /// So you think you have a problem...
        /// </summary>
        internal static readonly Regex RegexForValidatingUserQuery = GetRegex("RegexForValidatingUserQuery", @"^[\w -:\*]{1,256}$");

        /// <summary>
        /// This is what you'll get, stop whining
        /// </summary>
        internal static readonly int MaxNumberOfDocumentsToReturn = GetInt("MaxNumberOfDocumentsToReturn", 200);

        /// <summary>
        /// Keep the number of tasks processing file to a minimum or risk task queueing
        /// </summary>
        internal static readonly int MaxNumberOfTasksToProcessChangeset = GetInt("MaxNumberOfTasksToProcessChangeset", 4);

        /// <summary>
        /// Lifespan for jobs
        /// </summary>
        internal static readonly int MaxJobAgeInDays = GetInt("MaxJobAgeInDays", 7);

        /// <summary>
        /// Abandon job after this many retries
        /// </summary>
        internal static readonly int MaxJobRetries = GetInt("MaxJobRetries", 5);

        /// <summary>
        /// Tfs Registry key to the tfsgiggle uri, found by trial and error
        /// </summary>
        internal static readonly string TfsGiggleUri = GetString("TfsGiggleUri", "/Service/Registration/RegistrationExtendedAttribute/TfsGiggle/Uri");

        /// <summary>
        /// Only allow these file extensions to be indexed
        /// Please note that the stopwords should match the content in the files with the known extensions
        /// </summary>
        internal static readonly Regex RegexForKnownFileExtensions = GetRegex("RegexForKnownFileExtensions", @"^\.cs$|^\.cpp$|^\.h$|^\.hpp$|^\.js$|^\.cshtml$");
    }
}
